<template>
  <!-- 两层div包裹满足better-scroll的结构要求 -->
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
      <!-- border-topbottom是上下一像素边框 -->
        <div class="title border-topbottom">当前城市</div>
        <div class="btn-list">
          <div class="btn-wrapper">
            <div class="btn">{{this.currentCity}}</div>
            <!-- 当前城市的值和首页相同，取决于Vuex里的数据 -->
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="btn-list">
          <div class="btn-wrapper"
            v-for="item of hotCities"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="btn">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area"
        v-for="(item, key) of cities"
        :key="key"
        :ref="key"
      >
        <div class="title border-topbottom">{{key}}</div>
        <ul class="item-list">
          <li v-for="innerItem of item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >
            <div class="item border-bottom">{{innerItem.name}}</div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
// 导入better-scroll这个插件帮助我们实现滚动场景
import {mapState, mapMutations} from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
    // 把vuex里的公用数据city映射到名为currentCity的计算属性里
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
    // this.scroll不明白为什么这么用
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        // 我的理解是，普通情况下通过$refs.key或者$refs['key']，键名获取的键值就是对应的DOM节点
        // 而当ref与v-for结合使用时，通过键名获取的键值是一个数组，该数组的第一项才是DOM节点
        // 而Bscroll插件在使用scrollToElement方法时element要求必须是一个DOM节点
        this.scroll.scrollToElement(element)
        // 使用Bscroll的一个方法达到跳转的效果
      }
    }
  },
  methods: {
    handleCityClick (city) {
      this.changeCity(city)
      // 这里mapMutations辅助函数映射了changeCity，直接用

      // 或者this.$store.commit('changeCity', city)
      // 通过点击向Vuex触发commit方法(省略了dispatch)，去执行changeCity这个mutations

      this.$router.push('/')
      // 通过js编程式路由，点击后跳回首页
      // <router-link :to="...">这是声明式
    },
    ...mapMutations(['changeCity'])
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border-topbottom
    &:before
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    // 让整个List中超出屏幕的部分隐藏
    overflow: hidden
    // 设置绝对定位相当于限定了List的大小
    //满足了better-scroll插件的使用要求
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    .title
      line-height: .54rem
      padding-left: .2rem
      font-size: .26rem
      color: #666
      background: #eee
    .btn-list
      overflow: hidden
      padding: .1rem .3rem .1rem .1rem
      .btn-wrapper
        float: left
        width: 33.3%
        .btn
          margin: .1rem
          padding: .1rem 0
          text-align: center
          border: .02rem solid #ccc
          border-radius: .1rem
    .item-list
      .item
        padding: .2rem .4rem .2rem .2rem
</style>
